%% Central Limit Theroem

web('en.wikipedia.org/wiki/Central_limit_theorem')

%Make a vector of random numbers selected form a uniform distribution
%from -.5 to +.5

L=10000; %size of vector

a=rand(L,1)-.5;

%take a look with histogram:
figure
hist(a,100)
xlabel('bin center')
ylabel('counts')
title('a')


%make another and take a look:
b=rand(L,1)-.5;

figure
hist(b,100)
xlabel('bin center')
ylabel('counts')
title('b')

%now lets add together and take a look at the distribution of result:

c=a+b

figure
hist(c,100)
xlabel('bin center')
ylabel('counts')
title('c')
%The distribution looks diffferent!

%% Addding many vectors

%Lets take this to the extreme and add many vectors of random numbers
%together.

N=2;
L=100000;

A=rand(L,N)-0.5;

%If I sum along the 2nd dimension it is the same as adding N vectors
%together.
a = sum(A,2);
figure
hist(a,100)
xlabel('bin center')
ylabel('counts')
title('a')


%lets watch what happens as N is increased:

figure

for N=1:10

    %generate matrix
    A=rand(L,N)-0.5;
    
    %sum along 2nd dimension
    a = sum(A,2);

    hist(a,100)
    
    xlabel('bin center')
    ylabel('counts')
    title('a')
    
    pause(1)
end



%Now go to extreme!

N=1000;     %Extreme N, dude!
L=100000;

A=rand(L,N)-0.5;

%If I sum along the 2nd dimension it is the same as adding N vectors
%together.
a = sum(A,2);
figure
hist(a,100)
xlabel('bin center')
ylabel('counts')
title('a')


%% Try with much different distribution

N=2;
L=100000;

%This makes a bi-modal distribution with peaks at -.5 and .5
A=round(rand(L,N))-0.5;

figure;
hist(A(:))
xlabel('bin center')
ylabel('counts')

%lets watch what happens as N is increased:

figure

for N=1:50

    %generate matrix
    A=round(rand(L,N))-0.5;
    
    %sum along 2nd dimension
    a = sum(A,2);

    hist(a,100)
    
    xlabel('bin center')
    ylabel('counts')
    title('a')
    
    pause(.2)
end

%Now go to extreme!

N=1000;
L=100000;

A=round(rand(L,N))-0.5;

%If I sum along the 2nd dimension it is the same as adding N vectors
%together.
a = sum(A,2);
figure
hist(a,100)
xlabel('bin center')
ylabel('counts')
title('a')

%% Random Walks

%It is interesting to note that our bimodal distribution above describes a
%'Random Walk' in which at discrete time intervals we either take a step to
%the left or right with equal (50%) proability. 

T=100000; %number of time steps
DX=round(rand(T,1))-0.5;

%'DX' is now a vector of 'jumps' we take at each time interval. 
%Our position at time 't' is the sum of all previous jumps.
%We can calculate this using 'cumsum'

X=cumsum(DX);

%For a 2D random walk, the X and Y jumps are calculated independently

DY=round(rand(T,1))-0.5;
Y=cumsum(DY);

%Take a look at our 2D random walk trajectory:

close all
figure
plot(X,Y)
xlabel('X')
ylabel('Y')
legend('Random Walk')

%Try making new trajectoris by re-running this block

%A random walk can be used to describe the motion of a particle undergoing
%Brownian motion:


% web('http://en.wikipedia.org/wiki/Brownian_motion')















